#include "pch.h"
#include "Config.h"
#include "../Tinyxml2/tinyxml2.h"

Config::Config(const std::string &config_file)
    : config_file_(config_file)
{
    loadFile(config_file_);
}


Config::~Config()
{
}

void Config::loadFile(const std::string & config_file)
{
    tinyxml2::XMLDocument doc;
    auto ret = doc.LoadFile(config_file.c_str());
    if (ret != tinyxml2::XML_SUCCESS)
    {
        LOG(ERROR) << "load config file faild: " << config_file << ", error code is " << ret << std::endl;

        return;
    }

    auto service = doc.FirstChildElement("service");
    
    if (service != nullptr)
    {
		const char *id = service->Attribute("id");
		if (id != nullptr)
		{
			id_ = id;
		}

        port_ = service->IntAttribute("port");

		thread_number_ = service->IntAttribute("threadNumber", 4);

		read_count_ = service->IntAttribute("readCount", 500);

        auto database = service->FirstChildElement("database");

        if (database != nullptr)
        {
            database_.load(database);
        }
    }
}
